#include <iostream>
#include <future>
#include <chrono>
#include <thread>

int add(int num1, int num2) 
{
    std::cout << "add[1]\n";
    // std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "add[2]\n";
    return num1 + num2;
}

int main()
{
    // std::launch::deferred  get获取异步结果时，才会执行异步任务
    // std::launch::async    内部会创建工作线程，异步的完成任务
    std::cout << "--------1----------\n";
    // std::future<int> ret = std::async(std::launch::async, add, 1, 2);
    std::future<int> ret = std::async(std::launch::deferred, add, 1, 2);
    std::this_thread::sleep_for(std::chrono::seconds(1));

    std::cout << "--------2----------\n";
    // 获取异步任务的结果，任务未完成则进行阻塞
    // 即，在执行get()时，主线程与异步任务是同步的
    auto sum = ret.get();    

    std::cout << "--------3----------\n";
    std::cout << sum << '\n';
    return 0;
}